import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
import QtQuick.Dialogs 1.0
import QtQuick.XmlListModel 2.0


Rectangle {
    id: rectanglePaneCharacterClass
    anchors.margins: 6
    color: "transparent"


    Text {
        id: textCharacterTitle
        text: characterClassesModel.get(editIndex).name
        color: "white"
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        font.pixelSize: 16
        font.bold: true
    }


    Column {
        id: columnCharacterFeatures
        anchors.top: textCharacterTitle.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.margins: 2
        anchors.topMargin: 6
        spacing: 6

        Row {
            spacing: 3
            Text {
                text: qsTr("Hit Die:")
                color: "white"
                font.bold: true
            }
            Text {
                text: "d" + modelHitDice.get(characterClassesModel.get(editIndex).classHitDie).dieValue
                color: "white"
            }
        }
        Row {
            spacing: 3
            Text {
                text: qsTr("Skill Points:")
                color: "white"
                font.bold: true
            }
            Text {
                text: modelSkillPoints.get(characterClassesModel.get(editIndex).classSkillPoints).skillPoints
                color: "white"
            }
        }

        Column {
            anchors.left: parent.left
            anchors.right: parent.right
            Text {
                text: qsTr("Class Skills")
                color: "white"
                font.bold: true
            }

            Text {
                text: characterClassesModel.get(editIndex).classSkills.join(", ")
                color: "white"
                wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                width: parent.width
            }
        }
    }

    Rectangle {
        id: rectangleTitleRow

        anchors.top: columnCharacterFeatures.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.margins: 2
        anchors.topMargin: 12
        height: 20

        color: "darkred"
        border.color: "white"
        border.width: 1
        clip: true

        Row {
            anchors.fill: parent
            anchors.margins: 4
            spacing: 12

            Text {
                id: textLevelHeader
                text: "Level"
                anchors.verticalCenter: parent.verticalCenter
                //                    width: 50
                color: "white"
                font.bold: true

            }

            Text {
                id: textBaseAttackHeader
                text: "BA"
                anchors.verticalCenter: parent.verticalCenter
                //                    width: 50
                color: "white"
                font.bold: true
                horizontalAlignment: Text.AlignHCenter
            }

            Text {
                id: textFortSaveHeader

                text: "Fort"
                anchors.verticalCenter: parent.verticalCenter
                //                    width: 50
                color: "white"
                font.bold: true
                horizontalAlignment: Text.AlignHCenter
            }

            Text {
                id: textRefSaveHeader
                text: "Ref"
                anchors.verticalCenter: parent.verticalCenter
                //                    width: 50
                color: "white"
                font.bold: true
                horizontalAlignment: Text.AlignHCenter
            }

            Text {
                id: textWillSaveHeader

                text: "Will"
                anchors.verticalCenter: parent.verticalCenter
                //                    width: 50
                color: "white"
                font.bold: true
                horizontalAlignment: Text.AlignHCenter
            }

            Text {
                id: textSpecialAbilitiesHeader
                text: "Special Abilities"
                anchors.verticalCenter: parent.verticalCenter
                color: "white"
                font.bold: true
                width: parent.width - x
//                horizontalAlignment: Text.AlignRight
            }
        }

    } // rectangleTitleRow

    ScrollView {
        id: scrollViewCharacter
        anchors.top: rectangleTitleRow.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        //            anchors.bottom: rectangleFooterRow.top
        anchors.bottom: parent.bottom

        ListView {
            id: listViewCharacter

            /// \todo use actual model instead of magic number!
            /// \author Jussi
            /// \date 2013-10-10
            model: characterClassesModel.get(editIndex).levels

            delegate: Rectangle {
                id: rectangleCharacterRow

                property int classLevel: index + 1

                property int baseBonus: classLevel
                property int goodSave: classLevel/2
                property int badSave: classLevel/4
                anchors.margins: 2
                anchors.left: parent.left
                anchors.right: parent.right
                clip: true

                MouseArea {
                    id: mouseAreaCharacterRow
                    anchors.fill: parent
                    hoverEnabled: true
                    cursorShape: Qt.PointingHandCursor
                    onClicked: { levelIndex = index; }
                }


                Row {
                    width: parent.width
                    anchors.fill: parent
                    anchors.margins: 4
                    spacing: 12

                    Text {
                        id: textLevelCol
                        text: rectangleCharacterRow.classLevel
                        anchors.top: parent.top
                        width: textLevelHeader.width
                        color: "white"
                        font.bold: true
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignTop
                        lineHeight: 1.2
                    }

                    Text {
                        id: textBaseAttackCol
                        text: "+" + rectangleCharacterRow.baseBonus
                        anchors.top: parent.top
                        width: textBaseAttackHeader.width
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignTop
                        lineHeight: 1.2
                    }

                    Text {
                        id: textFortSaveCol
                        text: "+" + rectangleCharacterRow.goodSave
                        anchors.top: parent.top
                        width: textFortSaveHeader.width
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignTop
                        lineHeight: 1.2
                    }

                    Text {
                        id: textRefSaveCol
                        text: "+" + rectangleCharacterRow.badSave
                        anchors.top: parent.top
                        width: textRefSaveHeader.width
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignTop
                        lineHeight: 1.2
                    }

                    Text {
                        id: textWillSaveCol
                        text: "+" + rectangleCharacterRow.goodSave
                        anchors.top: parent.top
                        width: textWillSaveHeader.width
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignTop
                        lineHeight: 1.2
                    }

                    Text {
                        id: textSpecialAbilitiesCol

                        /// \todo Get special abilities from file
                        /// \author Jussi
                        /// \date 2013-10-11

//                        text:  editIndex%2 ? "Special Ablility" : index%2 ? "Special Ability" : "Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X, Special X"
                        text: characterClassesModel.get(editIndex).specialAbilities(classLevel).join(", ")
                        anchors.top: parent.top
                        width: textSpecialAbilitiesHeader.width
                        color: "white"
                        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                        verticalAlignment: Text.AlignTop
                        lineHeight: 1.2
                    }

                }

                height: textSpecialAbilitiesCol.height > 22 ? textSpecialAbilitiesCol.height * 1.2 : 22

                color: {
                    if (mouseAreaCharacterRow.containsMouse || levelIndex === index) {
                        "steelblue"
                    }
                    else if (index%2) {
                        "gray"
                    }
                    else {
                        "translparent"
                    }
                }

            } // rectangleTitleRow

        } // listViewSkills

    } // scrollViewSkills

}
